package lvcy.message.sender.builder.impl;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.JstMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.JstMessageSenderBuilder;

public class JstSingleMessageSenderBuilder extends JstMessageSenderBuilder{


	private Logger log=LogManager.getLogger(this);
	public MessageSender build(RequestEntity request) {
		try {
			Class<?> clazz=Class.forName(JstMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			this.sender=(MessageSender) constructor.newInstance("",getPostData(request));
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			log.error(e);
		}
		return this.sender;
	}
	private String getPostData(RequestEntity request){
		String opKind="0";
		String interFaceID="";
		String serType="短信类型";
		String sendTime=request.getExtras().get("sendTime")==null?"":request.getExtras().get("sendTime").toString();
		try {
			Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element group=document.createElement("Group");
			group.setAttribute("Login_name", request.getAccount());
			group.setAttribute("Login_Pwd", request.getPassword());
			group.setAttribute("OpKind", opKind);
			group.setAttribute("InterFaceID", interFaceID);
			group.setAttribute("SerType", serType);
			
			Element eTime=document.createElement("E_Time");
			eTime.setTextContent(sendTime);
			group.appendChild(eTime);
			
			Element item=document.createElement("Item");
			group.appendChild(item);
			
			Element task=document.createElement("Task");
			item.appendChild(task);
			
			if(request.getPhones().size()!=1){
				throw new IllegalArgumentException("");
			}
			Element recivePhoneNum=document.createElement("Recive_Phone_Number");
			recivePhoneNum.setTextContent(request.getPhones().get(0));
			task.appendChild(recivePhoneNum);
			
			Element content=document.createElement("Content");
			CDATASection cdataSection=document.createCDATASection(request.getContent());
			content.appendChild(cdataSection);
			task.appendChild(content);
			if(!request.getExtras().containsKey("clientId")){
				throw new IllegalArgumentException("the clientId must not be null");
			}
			Element searchId=document.createElement("Search_ID");
			searchId.setTextContent(request.getExtras().get("clientId").toString());
			task.appendChild(searchId);
			
			TransformerFactory transformerFactory=TransformerFactory.newInstance();
			Transformer transformer=transformerFactory.newTransformer();
			transformer.setOutputProperty("encoding", "UTF-8");
			ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
			transformer.transform(new DOMSource(group), new StreamResult(byteArrayOutputStream));
			return byteArrayOutputStream.toString();
			
		} catch (ParserConfigurationException | TransformerException e) {
			log.error(e);
		}
		return null;
	}
	

	

}
